// pages/prize-list/prize-list.js
var app = getApp();
const httpApi = require("../../http/request"); //后续可以改为你自己的域名接口地址
const config = require("../../http/env");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: config.baseUrl,
    type:'rule',
    user:'',
    tt_name:"规则",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that=this;
    //用户数据
    var type=options.type ? options.type : 'rule';
    var user = wx.getStorageSync("userInfo");

    if(type=='reward'){
      var tt_name='奖品';
    }else{
      var tt_name='规则';
    }

    //参数判断
    if (!user) {
      wx.redirectTo({
        url: "../home/home",
      });
    }

    that.setData({
      type:type,
      user:user,
      tt_name:tt_name,
    })

    that.getTis();
  },
  getTis() {
    var that=this;
    var param={
      user_id:that.data.user.id,
      type:that.data.type,
    };
    httpApi
      .Post("/index/getTis", param)
      .then((res) => {
        if (res.status == 0) {
					//成功返回
					var content=res.data;
					content = content.replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
                    .replace(/<p>/ig, '<p style="font-size: 14px;color: #fff;line-height: 25px;">')
                    .replace(/<span([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<span')
                    .replace(/<span>/ig, '<span style="font-size: 14px;color: #fff;line-height: 25px;">')
					          .replace(/<img([\s\w"-=\/\.:;]+)((?:(height="[^"]+")))/ig, '<img$1')
					          .replace(/<img([\s\w"-=\/\.:;]+)((?:(width="[^"]+")))/ig, '<img$1')
					          .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
					          .replace(/<img([\s\w"-=\/\.:;]+)((?:(alt="[^"]+")))/ig, '<img$1')
							      .replace(/<img([\s\w"-=\/\.:;]+)((?:(src=")))/ig, '<imgsrc="'+config.baseUrl)
					          .replace(/<img([\s\w"-=\/\.:;]+)/ig, '<img style="width: 100%;" $1');
					that.setData({
            content:content,
          })	  
				} else {
					//失败返回
					wx.showToast({
					  title: res.msg,
					  icon: 'none',
					  duration: 2000
					})
				}
      });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.data.queryParams.page++;
    this.setData({
      ["queryParams.page"]: this.data.queryParams.page,
    });
    console.log("queryParams", this.data.queryParams);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    var user=wx.getStorageSync("userInfo");
    var path='/pages/home/home?unitid='+user.unitid+'&unitid_copy='+user.unitid_copy + "&inviterId=" + wx.getStorageSync("userInfo").id;
    return {
      title: app.globalData.shareInfo[e.from].title,
      imageUrl: app.globalData.shareInfo[e.from].imageUrl,
      path: path,
      success: function(res) {
        // 转发成功
        console.log(11212);
      },
      fail: function(res) {
        // 转发失败
      }
    }
  },
});
